package com.bw.unit6

import android.text.Editable
import android.text.TextWatcher
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.unit3.base.BaseActivity
import com.bw.unit6.bean.GoodsBean
import com.bw.unit6.databinding.ActivityMainBinding
import com.bw.unit6.db.DbUtil
import com.bw.unit6.viewmodel.LoginViewModel

/**
 *
 * @ClassName: MainActivity
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/24 10:27 AM
 * @Version: 1.0
 */
@Route(path = "/main/MainActivity")
class MainActivity : BaseActivity<LoginViewModel, ActivityMainBinding>(R.layout.activity_main) {
    override fun initData() {
    }

    //存放所以的商品信息
    val data = mutableListOf<GoodsBean>()
    //用来存放搜索内容
    val dataSearch = mutableListOf<GoodsBean>()
    override fun initView() {
        val userInfo = DbUtil.dataBase.getUserInfoDao().getUserInfo()
        dataBinding.tvMoney.text = "当前余额：${userInfo.money}"

        //实现实时搜索，监听etSearch的输入内容，输入结束，继续搜索
        dataBinding.searchLayout.etSearch.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(content: Editable?) {
                //输入框，输入内容结束，回调该方法
                content.toString()//输入内容
                dataSearch.clear()
                //
                data.forEach {
                    //通过输入内容，跟goods_desc 字段进行匹配比较，保存符合搜索内容的数据
                    if(it.goods_desc.contains(content.toString())){
                        dataSearch.add(it)
                    }
                }



            }

        })
    }
}